<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">

      <el-form-item label="风场" prop="windId">
        <el-select v-model="queryParams.windId" placeholder="请选择风场" @change="handleChange">
          <el-option
            v-for="item in windList"
            :key="item.id"
            :label="item.windName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机组" prop="setId">
        <el-select v-model="queryParams.setId" placeholder="请选择机组" @change="handleQuery">
          <el-option
            v-for="item in setList"
            :key="item.id"
            :label="item.setName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产厂家设备型号" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入生产厂家设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:equipment:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:equipment:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:equipment:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:equipment:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id" width="50"/>
      <el-table-column label="设备名称" align="center" prop="equipmentName" width="80"/>
      <el-table-column label="设备类型" align="center" prop="equipmentType"/>
      <el-table-column label="生产厂家设备型号" align="center" prop="manufacturer"/>
      <el-table-column label="设备状态" align="center" prop="status"/>
      <el-table-column label="安装时间" align="center" prop="setupDate" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.setupDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="机组" align="center" prop="setId" width="50"/>
      <el-table-column label="风场" align="center" prop="windId" width="50"/>
      <el-table-column label="使用周期" align="center" prop="usePeriod"/>
      <el-table-column label="安装地点" align="center" prop="setupAddress"/>
      <el-table-column label="安装调试" align="center" prop="setupDebugg"/>
      <el-table-column label="试运行情况" align="center" prop="operateInfo"/>
      <el-table-column label="备件数量" align="center" prop="spareNum"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:equipment:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:equipment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风场设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="生产厂家设备型号" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入生产厂家设备型号"/>
        </el-form-item>
        <el-form-item label="安装时间" prop="setupDate">
          <el-date-picker clearable
                          v-model="form.setupDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择安装时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="机组id" prop="setId">
          <el-input v-model="form.setId" placeholder="请输入机组id"/>
        </el-form-item>
        <el-form-item label="风场id" prop="windId">
          <el-input v-model="form.windId" placeholder="请输入风场id"/>
        </el-form-item>
        <el-form-item label="使用周期" prop="usePeriod">
          <el-input v-model="form.usePeriod" placeholder="请输入使用周期"/>
        </el-form-item>
        <el-form-item label="安装地点" prop="setupAddress">
          <el-input v-model="form.setupAddress" placeholder="请输入安装地点"/>
        </el-form-item>
        <el-form-item label="安装调试" prop="setupDebugg">
          <el-input v-model="form.setupDebugg" placeholder="请输入安装调试"/>
        </el-form-item>
        <el-form-item label="试运行情况" prop="operateInfo">
          <el-input v-model="form.operateInfo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="备件数量" prop="spareNum">
          <el-input v-model="form.spareNum" placeholder="请输入备件数量"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment} from "@/api/system/equipment";
import {listWind} from "@/api/system/wind";
import {listSet} from "@/api/system/set";

export default {
  name: "Equipment",
  data() {
    return {
      windList: [],
      windId:'',
      setList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风场设备表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: null,
        manufacturer: null,
        setId: null,
        windId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        setId: [
          {required: true, message: "机组id不能为空", trigger: "blur"}
        ],
        windId: [
          {required: true, message: "风场id不能为空", trigger: "blur"}
        ],
        spareNum: [
          {required: true, message: "备件数量不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getWindList();
    this.getSetList();
  },
  methods: {
    handleChange(val) {
      console.log(val)
      this.windId = val;
      this.getSetList()
    },
    /** 查询风场设备列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWindList() {
      listWind({
        pageNum: 1,
        pageSize: 10000
      }).then(response => {
        console.log(response.rows)
        this.windList = response.rows;
      });
    },
    getSetList() {
      listSet({
          pageNum: 1,
          pageSize: 10000,
          setName: null,
          windId: this.windId
      }).then(response => {
        this.setList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentName: null,
        equipmentType: null,
        manufacturer: null,
        status: null,
        setupDate: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        setId: null,
        windId: null,
        usePeriod: null,
        setupAddress: null,
        setupDebugg: null,
        operateInfo: null,
        spareNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风场设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEquipment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改风场设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEquipment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除风场设备编号为"' + ids + '"的数据项？').then(function () {
        return delEquipment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/equipment/export', {
        ...this.queryParams
      }, `equipment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
